import { login, IData } from "../../view/login/api";
import { showSuccessToast, showFailToast } from "vant";
import router from "../../router";
const state = {
  // 用户信息
  user: {},
  routes: [],
};
const mutations = {
  // 修改用户信息
  SET_USER(state: any, payload: any) {
    state.user = payload;
  },
  SET_ROUTES(state: any, params: any) {
    state.routes = params;
  },
};
// interface IPayLoad extends IData {
//     path?: string;
// }
const actions = {
  LOGIN(context: any, payload: IData) {
    // 异步请求登录  将获取到的用户信息存储到store
    // console.log(2222)
    let { path, username, password } = payload;
    console.log(payload, 123);
    login({ username, password }).then((res) => {
      console.log(res, "login Data");
      if (res.data.meta.status == 200) {
        // 将用户信息存储到store
        context.commit("SET_USER", res.data.data);

        showSuccessToast(res.data.meta.msg);

        context.dispatch("GET_ROUTES");
        // 登陆成功 跳转页面
        setTimeout(() => {
          router.push(path || "/");
        }, 3000);
      } else {
        showFailToast(res.data.meta.msg);
      }
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
